<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :tree-node="company" :is-root="true" @dialogIsShowFn="dialogIsShowFn" />
        <hr>
        <el-tree
          :data="depts"
          :props="{label:'name'}"
          default-expand-all
        >
          <template #default="obj">
            <treeTools
              :tree-node="obj.data"
              @delDepartment="getDepartments"
              @dialogIsShowFn="dialogIsShowFn"
              @editDepartDetail="editDepartDetail"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <addDepartment
      ref="addDepatment"
      :dialog-is-show.sync="dialogIsShow"
      :tree-node="treeNode"
      @addDepartment="getDepartments"
    />
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { listToTreeData } from '@/utils'
import addDepartment from '@/views/departments/components/addDepartment.vue'
export default {
  components: {
    treeTools, addDepartment
  },
  data() {
    return {
      loading: true,
      dialogIsShow: false,
      treeNode: {},
      company:
        { name: '江苏传智播客教育科技股份有限公司',
          manager: '负责人',
          id: ''
        },
      depts: []
    }
  },
  async created() {
    await this.getDepartments()
    this.loading = false
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.depts = listToTreeData(depts, '')
    },
    dialogIsShowFn(treeNode) {
      this.dialogIsShow = true
      this.treeNode = treeNode
    },
    async editDepartDetail(treeNode) {
      // 在编辑函数前后加上控制等待图标显示的变量
      this.loading = true
      await this.$refs.addDepatment.editDepartDetail(treeNode.id)
      this.loading = false
      this.dialogIsShow = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
